home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7031 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  55 lines

  1. Path: newsstand.tc.umn.edu!ianhogg
  2. From: ianhogg@cs.umn.edu (Ian J Hogg)
  3. Newsgroups: comp.lang.c++
  4. Subject: Problem with enums and ostrstreams
  5. Date: 21 Feb 1996 16:43:57 GMT
  6. Organization: University of Minnesota
  7. Message-ID: <4gfi4d$kpj@epx.cis.umn.edu>
  8. NNTP-Posting-Host: tera.cs.umn.edu
  9.  
  10. I've been having a problem that the following code is a good example
  11. of:
  12.  
  13. #include <iostream.h>
  14. #include <strstream.h>
  15.  
  16. enum Codes { GOOD, BAD };
  17.  
  18. ostrstream & operator << (ostrstream &ostr, ostrstream & (*func)(ostrstream &))
  19. {
  20.     cout << "in ostrstream manip func" << endl;
  21.     return func(ostr);
  22. }
  23.  
  24. int main(int, char **)
  25. {
  26.     ostrstream msg;
  27.     msg << GOOD << ends;
  28.     cout << "msg = '" << msg.str() << "'" << endl;
  29.     return 0;
  30. }
  31.  
  32. 1. HPUX with HP C++ HPCPLUSPLUS  A.03.72 everthing is fine.
  33. 2. On solaris with C++4.0.1 it compiles cleanly but when GOOD is inserted
  34.    into the ostrstream, it calls the manip function.
  35. 3. OSF/1 (DEC Alpha), the compiler say "msg<<GOOD" is ambiguous.
  36.  
  37.  
  38. The two problems only occur when the enum's value is zero.  It appears to me
  39. that these compilers are treating the enums as literals. Is this behavior
  40. correct?  I've been able to work around the sun problem as follows:
  41.  
  42. ostrstream & operator << (ostrstream &ostr, ostrstream & (*func)(ostrstream &))
  43. {
  44.     if (func == 0) {
  45.        int zero = 0;
  46.        ostr << zero;
  47.     } else 
  48.        func(ostr);
  49.     return ostr;
  50. }
  51. --
  52. ===============================================================================
  53. Ian Hogg                        ianhogg@cs.umn.edu
  54.                                                         (612) 424-6332
  55.